home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / bison / InfixCalc < prev    next >
Text File  |  1995-06-28  |  3KB  |  89 lines

  1. Infix Calc
  2. Previous: <RPN Calc=>RPNCalc> * Next: <Simple Error Recovery=>SimpleErro> * Up: <Examples=>Examples>
  3.  
  4. #Wrap on
  5. {fH3}Infix Notation Calculator: {fCode}calc{f}{f}
  6.  
  7. We now modify rpcalc to handle infix operators instead of postfix.  Infix
  8. notation involves the concept of operator precedence and the need for
  9. parentheses nested to arbitrary depth.  Here is the Bison code for
  10. {fCite}calc.y{f}, an infix desk-top calculator.
  11.  
  12. #Wrap off
  13. #fCode
  14. \/\* Infix notation calculator--calc \*\/
  15.  
  16. %\{
  17. \#define YYSTYPE double
  18. \#include <math.h>
  19. %\}
  20.  
  21. \/\* BISON Declarations \*\/
  22. %token NUM
  23. %left '-' '+'
  24. %left '\*' '\/'
  25. %left NEG     \/\* negation--unary minus \*\/
  26. %right '^'    \/\* exponentiation        \*\/
  27.  
  28. \/\* Grammar follows \*\/
  29. %%
  30. input:    \/\* empty string \*\/
  31.         | input line
  32. ;
  33.  
  34. line:     '\\n'
  35.         | exp '\\n'  \{ printf ("\\t%.10g\\n", $1); \}
  36. ;
  37.  
  38. exp:      NUM                \{ $$ = $1;         \}
  39.         | exp '+' exp        \{ $$ = $1 + $3;    \}
  40.         | exp '-' exp        \{ $$ = $1 - $3;    \}
  41.         | exp '\*' exp        \{ $$ = $1 \* $3;    \}
  42.         | exp '\/' exp        \{ $$ = $1 \/ $3;    \}
  43.         | '-' exp  %prec NEG \{ $$ = -$2;        \}
  44.         | exp '^' exp        \{ $$ = pow ($1, $3); \}
  45.         | '(' exp ')'        \{ $$ = $2;         \}
  46. ;
  47. %%
  48. #f
  49. #Wrap on
  50.  
  51. The functions {fCode}yylex{f}, {fCode}yyerror{f} and {fCode}main{f} can be the same
  52. as before.
  53.  
  54. There are two important new features shown in this code.
  55.  
  56. In the second section (Bison declarations), {fCode}%left{f} declares token
  57. types and says they are left-associative operators.  The declarations
  58. {fCode}%left{f} and {fCode}%right{f} (right associativity) take the place of
  59. {fCode}%token{f} which is used to declare a token type name without
  60. associativity.  (These tokens are single-character literals, which
  61. ordinarily don't need to be declared.  We declare them here to specify
  62. the associativity.)
  63.  
  64. Operator precedence is determined by the line ordering of the
  65. declarations; the higher the line number of the declaration (lower on
  66. the page or screen), the higher the precedence.  Hence, exponentiation
  67. has the highest precedence, unary minus ({fCode}NEG{f}) is next, followed
  68. by {fEmphasis}\*{f} and {fEmphasis}\/{f}, and so on.  \*Note <Precedence=>Precedencf>: Operator Precedence.
  69.  
  70. The other important new feature is the {fCode}%prec{f} in the grammar section
  71. for the unary minus operator.  The {fCode}%prec{f} simply instructs Bison that
  72. the rule {fEmphasis}| '-' exp{f} has the same precedence as {fCode}NEG{f}---in this
  73. case the next-to-highest.  \*Note <Contextual Precedence=>Contextual>: Context-Dependent Precedence.
  74.  
  75. Here is a sample run of {fCite}calc.y{f}:
  76.  
  77. #Wrap off
  78. #fCode
  79. % calc
  80. 4 + 4.5 - (34\/(8\*3+-3))
  81. 6.880952381
  82. -56 + 2
  83. -54
  84. 3 ^ 2
  85. 9
  86. #f
  87. #Wrap on
  88.  
  89.